/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.article;

import java.util.ArrayList;
import java.util.Collection;
import util.article.SizeDetails;

/**
 *
 * @author Dirk
 */
public class SizesDialog extends javax.swing.JDialog {
    
    private final static String[] SIZES = {"104", "116", "128", "140", "152", "164", "176", "34", "36", "38", "40", "42", "44", "46", "48", "50", "50", "52", "54", "56", "58", "60"};
    private ArrayList<SizeDetails> sizeCollection;
    private boolean addSizes;
    /**
     * Creates new form SizesDialog
     */
    public SizesDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        addSizes = false;
        sizeCollection = new ArrayList<SizeDetails>();
        for(String size:SIZES)
            sizeCollection.add(new SizeDetails(-1, size));
        initComponents();
    }
    
    public boolean addSizes(){
        return addSizes;
    }
    
    public Collection<SizeDetails> getSelectedSizes(){
        return newListModel.getAllSizes();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        existingListModel = new SizeListModel(sizeCollection);
        existingSizesList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        newListModel = new SizeListModel();
        newSizeList = new javax.swing.JList();
        cancelBtn = new javax.swing.JButton();
        addBtn = new javax.swing.JButton();
        addSizeBtn = new javax.swing.JButton();
        removeSizeBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Selecteer de maten die je wilt toevoegen");

        existingSizesList.setModel(existingListModel);
        jScrollPane1.setViewportView(existingSizesList);

        newSizeList.setModel(newListModel);
        jScrollPane2.setViewportView(newSizeList);

        cancelBtn.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        cancelBtn.setText("annuleren");
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });

        addBtn.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        addBtn.setText("toevoegen");
        addBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBtnActionPerformed(evt);
            }
        });

        addSizeBtn.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        addSizeBtn.setText(">>>");
        addSizeBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addSizeBtnActionPerformed(evt);
            }
        });

        removeSizeBtn.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        removeSizeBtn.setText("<<<");
        removeSizeBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeSizeBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(addSizeBtn)
                            .addComponent(removeSizeBtn))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(addBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelBtn)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                            .addComponent(jScrollPane2)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(76, 76, 76)
                        .addComponent(addSizeBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(removeSizeBtn)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelBtn)
                    .addComponent(addBtn))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBtnActionPerformed
        addSizes = true;
        this.setVisible(false);
    }//GEN-LAST:event_addBtnActionPerformed

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
        addSizes = false;
        this.setVisible(false);
    }//GEN-LAST:event_cancelBtnActionPerformed

    private void addSizeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSizeBtnActionPerformed
        ArrayList<SizeDetails> toAdd = new ArrayList<SizeDetails>();
        for(Object size:existingSizesList.getSelectedValuesList()){
            toAdd.add((SizeDetails)size);
        }
        if(toAdd.size() > 0)
            newListModel.addSizes(toAdd);
    }//GEN-LAST:event_addSizeBtnActionPerformed

    private void removeSizeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeSizeBtnActionPerformed
        ArrayList<SizeDetails> toRemove = new ArrayList<SizeDetails>();
        for(Object size:newSizeList.getSelectedValuesList()){
            toRemove.add((SizeDetails)size);
        }
        if(toRemove.size() > 0)
            newListModel.removeSizes(toRemove);
    }//GEN-LAST:event_removeSizeBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SizesDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SizesDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SizesDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SizesDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SizesDialog dialog = new SizesDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addBtn;
    private javax.swing.JButton addSizeBtn;
    private javax.swing.JButton cancelBtn;
    SizeListModel existingListModel;
    private javax.swing.JList existingSizesList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    SizeListModel newListModel;
    private javax.swing.JList newSizeList;
    private javax.swing.JButton removeSizeBtn;
    // End of variables declaration//GEN-END:variables
}
